<?php

class IndexAction extends Action
{
    public function index()
    {
 		if(!isset($_SESSION['adminid']))
 		{ 
 			 $this->redirect('login');return;
 		}
 		$this->display();
    }
    public function login()
    {
    	if(!$this->isPost())
    	{
    		$this->display();
    	}
    	else
    	{
    		$verify=isset($_POST['verify'])?trim($_POST['verify']):'';
    		if(md5($verify)!=Session::get('verify'))
    		{
    			$msg="验证码错误或已经过期";$this->message($msg);return;
    		}
    		
    		$username=isset($_POST['username'])?trim($_POST['username']):'';
    		if(strlen($username)<6 || strlen($username)>16)
    		{
    			$msg="用户名为6到16个字符组成";$this->message($msg);return;
    		}
    		$password=isset($_POST['password'])?trim($_POST['password']):'';
    		if(strlen($password)<6 || strlen($password)>16)
    		{
    			$msg="用户密码为6到16个字符组成";$this->message($msg);return;
    		}

    		$Admin=D("Admin");
    		$conditions['username']=$username;
    		$temp=$Admin->find($conditions);    		
    		if(!$temp)
    		{
    			$msg="用户不存在或输入错误";$this->message($msg);return;
    		}
    		if(md5($password)!=$temp['password'])
    		{
    			$msg="密码输入错误";$this->message($msg);return;
    		}
    		    			
    		Session::set('adminid',(int)$temp['id']);
    		Session::set('username',$username);
    		Session::set('lasttime',$temp['lasttime']);
    		Session::set('lastip',$temp['lastip']);
			
			$this->redirect(); 		
    	}    
    }
    public function logout()
    {
    	
    	$Admin=D("Admin");
    	$data=array(
    		'id'=>Session::get('adminid'),
    		'lasttime'=>time(),
    		'lastip'=>get_client_ip(),
    	); 	
    	$Admin->save($data);
    	Session::clear();
    	Session::destroy();    
    	$this->redirect();	
    }    
    private function message($msg)
    {
    	$this->assign('msg',$msg);
    	$this->display('Index:message');
    	redirect(__URL__,2);
    }    
	public function verify() 
	{
        import("ORG.Util.Image");
       	Image::buildImageVerify();       	    	
	}    
}
?>